package cn.seaboot.admin.images.web;

import cn.seaboot.admin.images.bean.SysImage;
import cn.seaboot.admin.file.image.ImageManager;
import cn.seaboot.admin.images.service.SysImageService;
import cn.seaboot.admin.mvc.Pagination;
import cn.seaboot.admin.mvc.argument.Params;
import cn.seaboot.admin.user.context.UserContext;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 文件上传E.G.
 * <p>
 *
 * @author Mr.css on 2018-6-20
 */
@Controller
@Tag(name = "图片 [t_image_source] 管理")
@RequestMapping("sys/image/source")
public class SysImageCtrl {

    @Resource
    private SysImageService sysImageService;

    @Resource
    private UserContext userContext;

    @Resource
    private ImageManager imageManager;

    /**
     * 图片上传
     *
     * @param file 文件
     * @return SysImage
     * @throws IOException 文件未找到
     */
    @ResponseBody
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    @Operation(description = "图片 [t_image_source] 上传文件")
    public SysImage upload(@RequestParam MultipartFile file,
                           @RequestParam(defaultValue = "0") Integer type) throws IOException {
        return imageManager.upload(type, file);
    }

    /**
     * 图片下载
     *
     * @param id       文件ID
     * @param request  -
     * @param response -
     */
    @RequestMapping(value = "download/{id}", method = RequestMethod.GET)
    @Operation(description = "图片 [t_image_source] 显示图片")
    public void download(HttpServletRequest request, HttpServletResponse response, @PathVariable Long id) throws IOException {
        imageManager.download(request, response, id);
    }

    /**
     * 图片下载
     *
     * @param id       文件ID
     * @param request  -
     * @param response -
     */
    @RequestMapping(value = "preview/{id}", method = RequestMethod.GET)
    @Operation(description = "图片 [t_image_source] 显示图片")
    public void preview(HttpServletRequest request, HttpServletResponse response, @PathVariable Long id) throws IOException {
        imageManager.preview(request, response, id);
    }


    /**
     * 删除图片
     *
     * @param id ID
     * @return effect rows
     * @throws IOException 文件未找到
     */
    @ResponseBody
    @RequestMapping(value = "info", method = RequestMethod.DELETE)
    @Operation(description = "图片 [t_image_source] 删")
    @Parameters(@Parameter(name = "id", description = "ID"))
    public int deleteById(@Params Long id) throws IOException {
        return sysImageService.deleteById(id);
    }

    /**
     * 查询文件列表
     *
     * @return list
     */
    @ResponseBody
    @Operation(description = "图片 [t_image_source] 查询列表")
    @Parameters({
            @Parameter(name = "page", description = "页号"),
            @Parameter(name = "limit", description = "行数")})
    @RequestMapping(value = "page", method = RequestMethod.GET)
    public PageInfo<SysImage> queryList(@RequestParam Map<String, Object> params) {
        String userId = userContext.getUserId();
        return Pagination.get(params, () -> sysImageService.queryList(userId));
    }
}
